﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace CellarDoor.Gui.Elements
{
    public class UiMenu : UiElement
    {
        private UiMenuItem activeItem;
        private bool activeItemOpen;

        public UiMenu()
            : base()
        {
            Initialize();
        }
        
        public UiMenu(UiElement parent)
            : base(parent)
        {
            Initialize();
        }

        void Initialize()
        {
            Elements.ControlAdded += new EventHandler<UiElementEventArgs>(Elements_ControlAdded);
            Elements.ControlRemoved += new EventHandler<UiElementEventArgs>(Elements_ControlRemoved);
        }

        void Elements_ControlRemoved(object sender, UiElementEventArgs e)
        {
            e.Element.Click -= Element_Click;
        }

        void Elements_ControlAdded(object sender, UiElementEventArgs e)
        {
            e.Element.Click += Element_Click;
        }

        void Element_Click(object sender, UiEventArgs args)
        {
            if (sender == args.Target && sender is UiMenuItem)
            {
                if (activeItem != args.Target)
                {
                    activeItemOpen = true;
                }
                else
                {
                    activeItemOpen = !activeItemOpen;
                }
                activeItem = sender as UiMenuItem;
            }
        }

        public virtual UiMenuItem AddMenuItem(UiMenuItem item)
        {
            item.Dock = UiDockStyles.Top;
            item.Height = 20;
            item.SubMenu.Visible = false;
            item.SubMenu.Opacity = 0;
            return item;
        }

        public virtual UiMenuItem AddMenuItem(string text)
        {
            var item = new UiMenuItem(this);
            item.Text = text;
            return AddMenuItem(item);
        }

        public override void Update(GameTime gameTime)
        {
            if (Enabled && Visible)
            {
                foreach (var item in Elements)
                {
                    var menuItem = item as UiMenuItem;
                    if (menuItem != null && menuItem != activeItem && menuItem.SubMenu.Visible)
                    {
                        menuItem.SubMenu.Close();
                    }

                    if (activeItem != null)
                    {
                        if (activeItemOpen)
                        {
                            if (!activeItem.SubMenu.Visible && activeItem.SubMenu.Elements.Count > 0)
                            {
                                activeItem.SubMenu.Show();
                            }
                        }
                        else
                        {
                            if (activeItem.SubMenu.Visible)
                            {
                                activeItem.SubMenu.Close();
                            }
                        }
                    }
                }
            }

            base.Update(gameTime);
        }

        public virtual void Show()
        {
            this.Visible = true;
            this.Opacity = 1;
        }

        public virtual void Close()
        {
            this.Visible = false;
            this.Opacity = 0;
        }
    }
}
